<?php
/**
 * Created by JetBrains PhpStorm.
 * User: developer2
 * Date: 8/31/12
 * Time: 5:21 PM
 * To change this template use File | Settings | File Templates.
 */
class Db_Accounts_UserGroups extends Common {
	protected static $tableName = TAB_USER_GROUPS;
	protected static $dbFields = array(
		'id',
		'ug_name',
		'ug_permissions',
		'add_date',
		'add_user',
		'modified_date',
		'modified_user',
		'deleted'
	);
	/**
	 * @var Db_Accounts_UserGroups
	 */
	public $objectArray;

	public $id;
	public $ug_name;
	public $ug_permissions;
	public $deleted;

	function __construct($value=NULL, $fieldName=NULL) {
		parent::__construct($value, $fieldName);
	}

    public static function getName($userGroupId) {
        return self::$DB->selectCell('SELECT `ug_name` FROM ?f WHERE `id` = ?d AND `deleted` = 0;', static::$tableName, $userGroupId);
    }

	/**
	 * @static
	 * @return mixed
	 */
	public static function getAllUserGroups(){
		$sql = 'SELECT `ug`.*, IF(`ug`.`id` = `u`.`u_group_id`, 1, 0) AS `is_used`  FROM ?f AS `ug`
				LEFT JOIN ?f AS `u` ON `ug`.`id` = `u`.`u_group_id` AND `u`.`deleted` = 0
				WHERE `ug`.`deleted` = 0
				GROUP BY `ug`.`id` ;';
		return self::$DB->select($sql, self::$tableName, TAB_USERS);
	}

}
